package ua.sumdu.LukyanovChalyi.Server;

import java.text.SimpleDateFormat;
import java.util.Date;

public class CopyBook {

    private long id;
    private Book book;
    private boolean active;

    public CopyBook(Book book, long id, boolean isOnHand) {
        this.book = book;
        active = isOnHand;
        this.id = id;
    }

    public CopyBook(Book book, boolean isOnHand) {
        this(book, 0, isOnHand);
        id = this.hash();
    }

    public long getID() {
        return id;
    }

    public void setActive(boolean active) {
        this.active = active;
    }

    public Book getBook() {
        return book;
    }

    public boolean isActive() {
        return active;
    }

    @Override
    public boolean equals(Object other) {
        if (this == other) {
            return true;
        } else if (other == null) {
            return false;
        } else if (!getClass().equals(other.getClass())) {
            return false;
        } else {
            CopyBook copyBook = (CopyBook) other;
            return id == copyBook.id;
        }
    }

    @Override
    public int hashCode() {
        int hash = 3;
        hash = 83 * hash + (int) (this.id ^ (this.id >>> 32));
        hash = 83 * hash + (this.book != null ? this.book.hashCode() : 0);
        hash = 83 * hash + (this.active ? 1 : 0);
        return hash;
    }

    private long hash() {
        SimpleDateFormat sdf =
                new SimpleDateFormat("yyMMddHHmmssSS");
        StringBuilder sb = new StringBuilder();
        sb.append(sdf.format(new Date()).toString()).append((int) Math.random() * 100);
        return Long.parseLong(sb.toString());
    }

    @Override
    public String toString() {
        return "ID: " + id + ";\n" + book.toString()
                + "Active: " + active + ".";
    }
}